package ru.amse.podolsky.fractalarch.model;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Алексей
 */

//package ru.amse.podolsky.fractalarch;

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;


public class Block {

    //Constants
    private final int blockSize = IParams.BLOCK_SIZE;
    private final int domensPerBlock = 4;
    private final int domenSize = IParams.DOMEN_SIZE;
    private final int maxPixelIntensive = 255;

    //координаты левого верхнего угла
    private final int xcoordinate;
    private final int ycoordinate;
    private final int[][] pixels;
    
    public Block(BufferedImage buff, int x, int y){
        this.xcoordinate = x;
        this.ycoordinate = y;
        this.pixels = new int[this.blockSize][this.blockSize]; 
        for(int i = 0; i < this.blockSize; ++i){
            for(int k = 0; k < this.blockSize; ++k){
            this.pixels[i][k] = buff.getRGB(x + i, y + k) & maxPixelIntensive;
            }
        }
    }
    
    public int[][] getPixels(){
        return this.pixels;
    }
    
    public int getPixel(int x, int y){
        return this.pixels[x][y];
    }
    
    public int getX(){
        return this.xcoordinate;
    }
    
    public int getY(){
        return this.ycoordinate;
    }
    
    public Domen[] getDomens(){
        Domen [] domens = new Domen[this.domensPerBlock];
        for(int i = 0; i < this.domensPerBlock; ++i){
        domens[i] = new Domen(this, i / 2, i % 2);
        }
        return domens;        
    }
    
    public Domen getDomen(int number){
        return new Domen(this,  number / 2, number % 2);        
    }
    
    public Domen compress(){
        Domen domen = new Domen();
        
        for(int i = 0; i < this.domenSize; ++i){
            for(int k = 0; k < this.domenSize; ++k){
                int s = (this.getPixel(2 * i, 2 * k)
                        + this.getPixel(2 * i + 1, 2 * k + 1)
                        + this.getPixel(2 * i, 2 * k + 1)
                        + this.getPixel(2 * i + 1, 2 * k)) / 4;

                s = (s*2)/3;
                domen.setPixel(i, k, s);
            }
        }
        
        return domen;
    }

    public boolean compare(Block bl){
        for(int i = 0; i < this.domenSize; ++i){
            for(int k = 0; k < this.domenSize; ++k){
                if(this.getPixel(i, k) != bl.getPixel(i, k)){
                    return false;
                }
            }
        }
        return true;
    }
}
